package com.elock.service.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.elock.base.core.Filter;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.Terminal;
import com.elock.entity.TerminalMessageSend;
import com.elock.entity.TerminalTask;
import com.elock.service.MemberService;
import com.elock.service.TerminalMessageSendService;
import com.elock.service.TerminalService;
import com.elock.service.TerminalTaskService;
import com.elock.ts.pojo.MessageTemporary;
import com.elock.util.MqUtil;

/**
 * 终端信息下发
 * @author 仁德
 */
@Service("terminalMessageSendServiceImpl")
public class TerminalMessageSendServiceImpl extends BaseServiceImpl<TerminalMessageSend>
		implements TerminalMessageSendService {

	@Resource(name = "terminalServiceImpl")
	private TerminalService terminalService;
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	@Resource(name = "terminalTaskServiceImpl")
	private TerminalTaskService terminalTaskService;

	@Override
	@Transactional
	public Long sendMsg(Long terminalId, Integer type, String content,
			HttpServletRequest request) throws Exception {

		Terminal terminal = null;
		String terminalNum = null;
		List<Filter> filters = new ArrayList<Filter>();
		if (terminalId != null) {
			terminal = terminalService.find(terminalId);
			terminalNum = terminal.getTerminalNum();
		}
		else {
			terminalNum = request.getParameter("terminalNum");
			filters.add(Filter.eq("terminalNum", terminalNum));
			terminal = terminalService.find(filters);
		}

		String operationContent = content;
		String elockNo = null;
		String taskSn = null;
		if (type == 0x8103) {
			HashMap<Integer, String> map = new HashMap<Integer, String>();
			String heartbeat = request.getParameter("heartbeat");
			if (!ConvertUtil.isEmpty(heartbeat)) {
				map.put(0x0001, heartbeat);
			}
			String serverAddr = request.getParameter("serverAddr");
			if (!ConvertUtil.isEmpty(serverAddr)) {
				map.put(0x0017, serverAddr);
			}
			String tcpPort = request.getParameter("tcpPort");
			if (!ConvertUtil.isEmpty(tcpPort)) {
				map.put(0x0018, tcpPort);
			}
			String udpPort = request.getParameter("udpPort");
			if (!ConvertUtil.isEmpty(udpPort)) {
				map.put(0x0019, udpPort);
			}
			String gpsTime = request.getParameter("gpsTime");
			if (!ConvertUtil.isEmpty(gpsTime)) {
				map.put(0x0021, gpsTime);
			}
			if (map.isEmpty()) {
				ExceptionUtil.throwServiceException("请选择操作类型");
			}
			operationContent = JsonUtils.toJson(map);
		}
		else if (type == 0x8700) {
			elockNo = request.getParameter("elockNo");
			String waybillNo = request.getParameter("waybillNo");
			if (!ConvertUtil.isEmpty(waybillNo)) {
				filters.clear();
				filters.add(Filter.eq("waybillNo", waybillNo));
				TerminalTask terminalTask = terminalTaskService.find(filters);
				if (terminalTask == null) {
					ExceptionUtil.throwServiceException("根据输入的任务单编号找不到任务单");
				}
				taskSn = terminalTask.getSn();
			}

		}
		TerminalMessageSend messageSend = new TerminalMessageSend();
		messageSend.setTerminalNum(terminalNum);
		messageSend.setSendMessage(operationContent);
		messageSend.setSendType(type);
		messageSend.setSendResult(0);
		messageSend.setReplyResult(0);
		this.save(messageSend);

		MessageTemporary temporary = new MessageTemporary();
		temporary.setTerminalNum(terminalNum);
		temporary.setSim(terminal.getSim());
		temporary.setOperationType(type);
		temporary.setOperationContent(operationContent);
		temporary.setProcessor(memberService.getCurrent().getUsername());
		temporary.setMessageSendId(messageSend.getId());
		temporary.setCompanyInfoId(terminal.getCompanyInfoId());
		temporary.setElockNo(elockNo);
		temporary.setTaskSn(taskSn);

		MqUtil.sendToMQ(temporary);

		return messageSend.getId();
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> getMessageSend(Long id) {

		String sql = "select * from terminal_message_send where id = ?";
		Map<String, Object> sendMap = getDaoCenter().getNativeDao().findSingleMap(sql,
				new Object[] { id });

		return sendMap;
	}
}
